<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Validate;
use think\Db;
use app\admin\model\AgentModel;
class AgentController extends AdminBaseController
{
     public function index()
     {
          return $this->fetch();
     }
      public function agentlist()
     {
          $partmentinfo=Db::name('agent_list')->select()->toArray();
          return $this->fetch('Agent/list',[
                'partmentinfo'=>$partmentinfo
            ]);
     }
     //修改页面
     public function agentlistedit()
     {
          $data = $this->request->param();
          $res=AgentModel::where('id',$data['id'])->find();
          return $this->fetch('Agent/listedit',[
                'res'=>$res
            ]);
     }
     //修改操作
     public function agentlisteditt()
     {
          $data = $this->request->param();
          $time = time();
          $level=new AgentModel();
          $add=$level->where('id',$data['id'])->update(['create_time'=>$time,'name'=>$data['name'],'phone'=>$data['phone'],'idcard'=>$data['idcard'],'city'=>$data['city'],'product'=>$data['product'],'status'=>$data['status']]);
          if($add == 1)
          {
            $this->success('修改成功');
          }else{
            $this->error('修改失败');
          }
     }
      //删除操作
     public function agentlistdelete()
     {
            $data = $this->request->param();
            $res=AgentModel::destroy($data['id']);
            if($res ==1){
                $this->success('删除成功');
            }else{
                $this->error('添加失败');
            }
     }
      //代理筛选
     public function screen()
    {
        $data = $this->request->param();
        if($data['keyword'] != "")
        {

            $map['name'] = array('like', '%' . $data['keyword'] . '%');
            $listinfo=Db::name('agent_list')->where($map)->select()->toArray();
        }else if($data['category'] !=""){
            $whereArr['agentid'] =  array('neq',0); // 不等于条件
            $listinfo=Db::name('agent_list')->where('status',$data['category'])->select()->toArray();
            $levelinfo=Db::name('hospital_level')->where($whereArr)->select()->toArray();
        }else{
             $whereArr['agentid'] =  array('neq',0); // 不等于条件
             $listinfo=Db::name('agent_list')->select()->toArray();
             $levelinfo=Db::name('hospital_level')->where($whereArr)->select()->toArray();
        }
        return $this->fetch('Agent/list',[
            'partmentinfo'=>$listinfo
        ]);

    }
}
